Skip to main content

Workflow Loop Template

The Workflow Loop template demonstrates how a BindAI workflow can repeatedly execute a workflow path while a predicate evaluates to True. A LoopNode evaluates a predicate against the current workflow context. When the predicate is true, execution is directed to the loop body. When it is false, execution is directed to the loop exit. This template introduces iterative workflow execution.

Purpose

This template demonstrates how to:
  • Add a LoopNode to a workflow
  • Evaluate a runtime predicate
  • Execute a loop body repeatedly
  • Maintain state through the workflow context
  • Exit the loop when the predicate becomes false
  • Build iterative workflow automation

Workflow Structure

A LoopNode requires two next nodes:
More precisely, the LoopNode interprets its next_nodes as:
This ordering is part of the current LoopNode implementation.

LoopNode

A loop is created with a node ID and a predicate:
The predicate receives the current workflow context:
For example:
The predicate determines whether execution should continue through the loop body or leave the loop.

Configuring the Loop

The loop must have at least two next nodes. The first node is the loop body:
The second node is the exit:
Conceptually:
If fewer than two next nodes are configured, the loop raises:

Execution Flow

When the executor reaches the loop, the LoopNode evaluates its predicate.
The loop does not directly execute the selected node. Instead, it sets:
to the appropriate node ID. The workflow executor then uses that value to determine the next node to execute.

Loop Predicate

The predicate receives the workflow context. For example:
The predicate returns:
  • True to execute the loop body
  • False to execute the exit node
The predicate is evaluated each time the LoopNode executes.

Workflow Variables

Loop predicates commonly use workflow variables to track progress. For example:
A loop body could update the counter:
The conceptual execution becomes:
The actual state changes depend on the nodes used in the workflow.

Basic Example

A simple loop can be structured conceptually as:
This means:
When the predicate returns True, the workflow proceeds to process-item. When the predicate returns False, the workflow proceeds to loop-complete.

Iterative Processing

A loop is useful when a workflow needs to repeatedly process data. For example:
The process node can update the workflow context so that the next loop evaluation sees the new state.

Batch Processing

Loops can be used for collection-oriented workflows. Conceptually:
A variable such as an index can determine whether another item remains. For example:
The loop body can process the current item and advance the index.

Polling

Loops can also represent repeated checks of runtime state. For example:
The predicate can determine whether the workflow should continue checking. The loop itself does not provide a built-in sleep or delay operation. If waiting between checks is required, that behavior must be provided by the surrounding workflow implementation.

Shared Workflow Context

Each workflow execution operates with a workflow context. The loop predicate receives that context directly:
The loop body can update the same execution state. Conceptually:
Variables remain available to later nodes unless they are explicitly changed.

Loop Exit

The loop exits when the predicate returns False.
The exit node is next_nodes[1]. The exit node can then connect to additional workflow nodes. For example:
There is no special built-in EndNode required for a loop exit.

Loop vs Condition

A condition selects a path once when it executes. A loop can repeatedly evaluate its predicate. Both use the workflow context to make runtime decisions.

Loop vs Retry

Loops and retries both involve repeated execution, but they serve different purposes. Use a loop when repetition is part of the business process. Use a retry policy when an operation fails and should be attempted again.

Preventing Infinite Loops

A loop should have a predicate that can eventually become false. For example:
The loop body must update the state used by the predicate:
Without a state change, a predicate such as:
could remain true indefinitely. A useful pattern is:

Error Handling

The LoopNode itself raises an error if it does not have two next nodes:
Errors raised during execution of the loop body are handled by the workflow executor. If no retry policy is configured, the workflow fails. If a retry policy is configured, the executor can retry the failed node according to the configured attempt limit. Loop behavior and retry behavior are separate mechanisms.

Combining Loops with Conditions

Loops and conditions can be combined. For example:
The exact routing depends on the node relationships configured in the workflow. A condition can be used inside a loop body to make additional decisions.

Combining Loops with Agents

An agent can participate in the loop body when repeated AI processing is required. For example:
The agent can process information and the surrounding workflow can update context variables before the next loop evaluation. The LoopNode itself does not perform AI operations.

Current Implementation Details

The current implementation has the following behavior:
It stores:
During execution it first verifies that two next nodes exist:
It then evaluates:
When the predicate is true:
When the predicate is false:
The WorkflowExecutor subsequently uses context.current_node when determining what node to place into the execution queue.

Important Boundaries

Two next nodes are required

The current implementation requires at least two entries in next_nodes. They represent:

The predicate receives the context

The loop predicate receives the complete workflow context:
It is not passed a single variable automatically.

The loop does not execute its body directly

The loop sets context.current_node. The workflow executor is responsible for continuing execution.

No built-in delay

The LoopNode does not provide a built-in waiting period between iterations. Polling workflows that require delays need another mechanism for waiting.

No automatic iteration counter

The LoopNode does not maintain a built-in iteration counter. If a workflow needs one, store it in the workflow context or another appropriate state mechanism.

Best Practices

  • Always configure both the loop body and exit node.
  • Make the predicate deterministic and easy to understand.
  • Update the state used by the predicate during the loop body.
  • Ensure the loop has a realistic termination condition.
  • Keep loop bodies focused.
  • Use retries for transient failures rather than business-process repetition.
  • Test both loop continuation and loop exit.
  • Test edge cases such as an empty collection or an already-satisfied condition.
  • Avoid unnecessary nested loops.
  • Add observability for workflows containing long or complex loops.

Related Templates

The loop pattern builds on the basic workflow model introduced in:
  • 08.1 workflow-basic.md
It can be combined with:
  • 08.2 workflow-condition.md
  • 08.4 workflow-parallel.md
  • 08.5 workflow-human.md
  • 08.6 workflow-retry.md
  • 08.7 workflow-timeout.md
  • 08.8 workflow-schedule.md

Summary

The Workflow Loop template introduces iterative execution into BindAI workflows. The essential pattern is:
A LoopNode evaluates a predicate against the workflow context. If the predicate returns True, the first configured next node is selected as the loop body. If the predicate returns False, the second configured next node is selected as the loop exit. The workflow executor then continues execution using the selected node. This provides a simple mechanism for repeated business processing, collection handling, polling-style workflows, and other iterative workflow patterns.